package com.bvgol.examples.dubbo.zk.fanhua;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.rpc.service.GenericException;
import com.alibaba.dubbo.rpc.service.GenericService;

public class DubboTest {
    public static void main(String[] args) {
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        // 当前dubbo consumer的application配置，不设置会直接抛异常
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("zk-consumer");
        // 注册中心配置
        RegistryConfig registryConfig = new RegistryConfig();
        // 注册中心这里需要配置上注册中心协议，例如下面的zookeeper
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
//        registryConfig.setGroup("");
        reference.setApplication(applicationConfig);
        reference.setRegistry(registryConfig);
        // 设置调用的reference属性，下面只设置了协议、接口名、版本、超时时间
//        reference.setProtocol("zookeeper");
        reference.setInterface("com.bvgol.examples.dubbo.zk.api.Hello3Service");
//        reference.setVersion("");
        reference.setTimeout(1000);
        // 声明为泛化接口
        reference.setGeneric(true);

        GenericService genericService = reference.get();
        // 通过 GenericService.$invoke 方法指定是泛化调用，方法名为 sayHello。参数类型为 String， 参数内容为 generic
        System.out.println("构造完毕准备调用");
        Object sayHello = null;
        try {
            sayHello = genericService.$invoke("testDubbo3", new String[]{"java.lang.String"}, new Object[]{"generic"});
        } catch (GenericException e) {
            e.printStackTrace();
        }
        System.out.println("testDubbo3 = " + sayHello);
    }
}
